package com.servlet;

import com.service.BlogService;
import com.service.BlogServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "BlogDelServlet",urlPatterns = {"/BlogDelServlet"})
public class BlogDelServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out =  response.getWriter();
		//1、获取请求的数据==》挑几件
		String id = request.getParameter("id");
		Integer idTmp = Integer.valueOf(id);
		//2、处理==》操作数据库
		BlogService blogService = new BlogServiceImpl();
		int ret = blogService.deleteById(idTmp);
		if(ret>0){
			out.println("<script>alert('删除成功');location.href='BlogListServlet'</script>");
		}else{
			out.println("<script>alert('删除失败');history.go(-1);</script>");
		}
		out.flush();
		out.close();
	}
}
